<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Member;
use app\model\Technical;
use app\model\Config;
use app\model\Category;
use app\model\Order;
use app\model\Openid;
use think\facade\Cache;
use app\model\TechnicalIncomelog;

class AdmintechnicalController extends Base
{

	public function check()
	{
		if (Cache::store('redis')->get('technical_' . UID())) {
			$data = null;
		} else {
			$data = Technical::where(['uid' => UID()])->find();
		}
		if (!empty($data)) {
			$this->updateuuid($data['uuid'], 'technical');
			$data = $data->toArray();
			if (!empty($data['uuid'])) {
				Technical::where('id', $data['id'])->update(['uuid' => $data['uuid']]);
			}
		} else {
			$uuid = $this->getuuid('technical');
			if (!empty($uuid)) {
				$data = Technical::where(['uuid' => $uuid])->find();
				if (!empty($data)) {
					$data = $data->toArray();
				}
			}
		}

		if(!Cache::store('redis')->get('technical_' . UID())){
			$data = 0;
			$message = '请先登录！';
			return $this->json(['message' => $message, 'data' => $data]);
		}
		if ($data['status'] != 1) {
			$data = 0;
			$message = '资料在审核中！';
		} else {
			$data['statistical'] = $this->statistical($data['uuid']);
		}
		return $this->json(['message' => $message, 'data' => $data,'uuid'=>$data['uuid']]);
	}
	public function statistical($uuid)
	{
		$query = Order::where(['weid' => weid(), 'ptype' => 2]);
		$withJoin = [
			'staffOne' => ['order_id', 'uuid', 'title', 'endTime', 'beginTime'],
		];
		$query->withJoin($withJoin, 'left');
		$query->where(['staffOne.uuid' => $uuid]);

		$query2 = $query;
		$statistical['status2'] = $query2->where('order_status_id', 2)->count();

		$query3 = $query;
		$statistical['status3'] = $query3->where('order_status_id', 3)->count();

		$query5 = $query;
		$statistical['status5'] = $query5->where('order_status_id', 5)->count();

		$query4 = $query;
		$statistical['status4'] = $query4->where('order_status_id', 4)->count();

		return $statistical;
	}
	public function business()
	{
		$id = $this->request->post('id');

		$Technical = Technical::find($id);

		if ($Technical->is_business == 1) {
			$Technical->is_business = 0;
		} else {
			$Technical->is_business = 1;
		}

		try {
			$Technical->save();
			$data = $Technical->toArray();
			$data['statistical'] = $this->statistical($data['uuid']);
		} catch (\Exception $e) {
			throw new ValidateException($e->getMessage());
		}
		return $this->json(['msg' => '操作成功', 'data' => $data]);
	}

	public function update()
	{
		$id = $this->request->post('id');
		$data = input('post.');

		try {
			Technical::update($data);
		} catch (\Exception $e) {
			throw new ValidateException($e->getMessage());
		}
		return $this->json(['msg' => '修改成功']);
	}

	function getInfo()
	{
		$uuid = $this->getuuid('technical');
		$data = Technical::where(['uuid' => $uuid])->find();
		if (!empty($data)) {
			// $data = $data->toArray();
		} else {
			$data = Technical::where(['uid' => UID()])->find();
			if (!empty($data)) {
				// $data = $data->toArray();
				if (!empty($uuid)) {
					Technical::where('id', $data['id'])->update(['uuid' => $uuid]);
				}
			}
		}

		if (!empty($data)) {
			// $data = $data->toArray();
		}
		$data['cate_ids'] = explode(',', $data['cate_ids']);
		$data['Category'] = Category::gettoparray();
		if (!empty($data['certificate'])) {
			$data['certificate'] = json_decode($data['certificate']);
		}
		if (!empty($data['casus'])) {
			$data['casus'] = json_decode($data['casus']);
		}

		return $this->json(['data' => $data]);
	}
	public function addPhoto()
	{

		$uuid = $this->getuuid('technical');
		$postdata = [];
		$certificate = input('post.certificate');
		if (!empty($certificate)) {
			$postdata['certificate'] = json_encode(explode(',', $certificate));
		}
		$casus = input('post.casus');
		if (!empty($casus)) {
			$postdata['casus'] = json_encode(explode(',', $casus));
		}

		Technical::where(['uuid' => $uuid])->update($postdata);
		return $this->json(['message' => '保存成功！', 'data' => $postdata]);
	}
	public function logout()
	{
		$uuid = $this->getuuid('technical');
		Openid::where(['uuid' => $uuid])->update(['status' => 1]);
		Cache::store('redis')->delete('technical_' . UID());
		return $this->json(['message' => '退出成功！']);
	}
	public function doBindUser()
	{
		$id = input('post.id');
		$type = input('post.type');
		$user_id = UID();
		if ($type) {
			Technical::where(['uid' => $user_id])->update(['uid' => 0]);
			Technical::where(['id' => $id])->update(['uid' => $user_id]);
			// Cache::store('redis')->delete('technical_' . UID());
			return $this->json(['message' => '绑定成功！']);
		} else {
			Technical::where(['id' => $id])->update(['uid' => 0]);
			return $this->json(['message' => '解绑成功！']);
		}
	}
	public function incomelog()
	{
		$uuid = $this->getuuid('technical');
		$ptype = input('post.ptype');

		$where['weid'] = weid();
		$where['uuid'] = $uuid;
		if ($ptype) {
			$where['ptype'] = $ptype;
		}

		$data = TechnicalIncomelog::where($where)
			->order('id desc')
			->paginate(getpage())->each(function ($item, $key) {
				$item['create_time'] = time_format($item['create_time']);
				return $item;
			});
		return $this->json(['data' => $data]);
	}
}
